package com.thekernel.youdaoyun.mapper;

import com.thekernel.youdaoyun.bean.Star;
import com.thekernel.youdaoyun.bean.User;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @Author TheKernel
 * @Date 2019/9/13 7:33 下午
 * @Version 1.0
 * @Description 操作 star 数据库
 */
@Component
@Mapper
public interface StarMapper {

    /**
     * 添加收藏
     * @param star 收藏的信息
     * @return 0: 添加失败, 1: 添加成功
     */
    @Insert("INSERT INTO `star`(`star_note`, `star_user`, `status`, `create_time`) " +
            "VALUES(#{starNote}, #{starUser}, #{status}, #{createTime})")
    @Options(useGeneratedKeys = true, keyProperty = "starId")
    int add(Star star);

    /**
     * 通过收藏 id 查询对应笔记
     * @param starId 收藏 id
     * @return star
     */
    @Select("SELECT * FROM `star` WHERE `star_id`=#{starId} AND `status`=1 LIMIT 1")
    @Results(
            id = "star",
            value = {
                    @Result(property = "starId", column = "star_id", javaType = long.class),
                    @Result(property = "starNote", column = "star_note", javaType = Long.class),
                    @Result(property = "starUser", column = "star_user", javaType = Long.class),
                    @Result(property = "status", column = "status", javaType = Boolean.class),
                    @Result(property = "createTime", column = "create_time", javaType = Date.class)
            }
    )
    Star queryStarByStarId(long starId);

    /**
     * 通过用户和笔记查询收藏id
     * @param star
     * @return
     */
    @Select("SELECT `star_id` FROM `star` WHERE `star_user`=#{starUser} AND `star_note`=#{starNote}")
    @Results(
            id = "starId",
            value = {
                    @Result(property = "starId", column = "star_id", javaType = long.class)
            }
    )
    Star queryStarIdByStarUserAndStarNote(Star star);

    /**
     * 取消收藏
     * @param starId 收藏 id
     * @return 1: 取消成功 0: 取消失败
     */
    @Delete("DELETE FROM `star` WHERE `star_id`=#{starId} LIMIT 1")
    int cancelStarByStarId(long starId);

    /**
     * 查看所有收藏
     * @param userId 用户id
     * @return 收藏列表
     */
    @Select("SELECT * FROM `star` WHERE `star_user`=#{userId} AND `status`=1")
    @Results(
            id = "stars",
            value = {
                    @Result(property = "starId", column = "star_id", javaType = long.class),
                    @Result(property = "starNote", column = "star_note", javaType = long.class),
                    @Result(property = "starUser", column = "star_user", javaType = long.class),
                    @Result(property = "createTime", column = "createTime", javaType = Date.class),
            }
    )
    Star[] queryAllStars(long userId);

    /**
     * 查看所有收藏
     * @param noteId 笔记id
     * @return 收藏
     */
    @Select("SELECT `star_id` FROM `star` WHERE `star_note`=#{noteId} AND `status`=1")
    @Results(
            id = "star_by_note",
            value = {
                    @Result(property = "starId", column = "star_id", javaType = long.class)
            }
    )
    Star queryStarIdByNoteId(long noteId);

    /**
     * 通过笔记 id 取消收藏
     * @param noteId 笔记 id
     * @return 1: 取消成功 0: 取消失败
     */
    @Delete("DELETE FROM `star` WHERE `star_note`=#{noteId} LIMIT 1")
    int cancelStarByNoteId(long noteId);

}
